import tiyData from "./booth_data"
import showroomData from "../../showroomSelect/js/showroomData";
import commonData from "./common_data";
import collectionData from "./collection_data"
import {afterModifyStandsBaseInfo, queryBoothList} from "./edit_funs";
import {isEmpty, isNotEmpty} from "../../../js/commonUtils";
import {initShowroomPage, initShowroomParams} from "../../showroomSelect/js/showroomFuns";
import productData from "../../productSelect/js/productData";
import {replaceHtmlTagAttr} from "../../../widgets/quill/extension";

/**重置页面信息*/
export const resetPageInfo = (self) => {
  self.isDisplayBaseInfo = true;
}

export const resetBoothItemListSearchParam = (boothId) => {
  tiyData.tParam.searchParam.boothItem.boothId = boothId;
  tiyData.tParam.searchParam.boothItem.targetType = 0;
}

/**初始化展区信息*/
export const initBoothInfo = (self, boothId, ifInitBoothItems) => {
  let params = {id: boothId};
  if(ifInitBoothItems) {
    tiyData.tData.boothItemList.splice(0);
  }
  self.$Spin.show();
  self.$apiStand.queryBoothById(params).then(response => {
    if(response.success) {
      let booth = response.result;
      if(booth == null) {
        self.$Message.error("未查询到数据，展区不存在或已被删除！");
        return;
      }
      //基本信息
      initBoothBaseInfo(booth);
      //展位列表
      if(ifInitBoothItems) {
        tiyData.tData.boothItemList.push(...booth.items);
      }
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**初始化展区基本信息*/
export const initBoothBaseInfo = (booth) => {
  tiyData.tData.boothBaseInfo.id = booth.id;
  tiyData.tData.boothBaseInfo.showroomId = booth.showroomId;
  tiyData.tData.boothBaseInfo.displayType = booth.displayType;
  tiyData.tData.boothBaseInfo.platform = booth.platform;
  tiyData.tData.boothBaseInfo.title = booth.title;
  tiyData.tData.boothBaseInfo.subtitle = booth.subtitle
  tiyData.tData.boothBaseInfo.sequence = booth.sequence;
  tiyData.tData.boothBaseInfo.columnPc = booth.columnPc;
  tiyData.tData.boothBaseInfo.columnMobile = booth.columnMobile;
  tiyData.tData.boothBaseInfo.widthPc = booth.widthPc;
  tiyData.tData.boothBaseInfo.heightPc = booth.heightPc;
  tiyData.tData.boothBaseInfo.widthMobile = booth.widthMobile;
  tiyData.tData.boothBaseInfo.heightMobile = booth.heightMobile;
  tiyData.tData.boothBaseInfo.rowNumber = booth.rowNumber;
  tiyData.tData.boothBaseInfo.note = booth.note;
  tiyData.tData.boothBaseInfo.createtime = booth.createtime;
  if(tiyData.tData.boothBaseInfo.displayType == commonData.boothDic.htmlType) {
    tiyData.tData.boothBaseInfo.htmlContent = booth.items[0].targetValue;
    tiyData.tData.boothBaseInfo.isCodeEdit = booth.items[0].isCodeEdit;
  }
}

/**重置展区修改表单参数*/
export const resetBoothEditParam = (self) => {
  self.$refs.BoothEdit.$refs.boothInfoForm.resetFields();
}

/**初始化展区修改表单参数*/
export const initBoothEditParam = (booth) => {
  tiyData.tParam.editParam.booth.id = booth.id;
  tiyData.tParam.editParam.booth.showroomId = booth.showroomId;
  tiyData.tParam.editParam.booth.displayType = booth.displayType;
  tiyData.tParam.editParam.booth.platform = booth.platform;
  tiyData.tParam.editParam.booth.title = booth.title;
  tiyData.tParam.editParam.booth.subtitle = booth.subtitle
  tiyData.tParam.editParam.booth.sequence = booth.sequence;
  tiyData.tParam.editParam.booth.columnPc = booth.columnPc;
  tiyData.tParam.editParam.booth.columnMobile = booth.columnMobile;
  tiyData.tParam.editParam.booth.widthPc = booth.widthPc;
  tiyData.tParam.editParam.booth.heightPc = booth.heightPc;
  tiyData.tParam.editParam.booth.widthMobile = booth.widthMobile;
  tiyData.tParam.editParam.booth.heightMobile = booth.heightMobile;
  tiyData.tParam.editParam.booth.rowNumber = booth.rowNumber;
  tiyData.tParam.editParam.booth.note = booth.note;
  tiyData.tParam.editParam.booth.htmlContent = booth.htmlContent;
  tiyData.tParam.editParam.booth.isCodeEdit = booth.isCodeEdit;
}

/**显示展区基本信息编辑弹窗*/
export const showModifyBoothBaseInfoModal = (self, booth) => {
  resetBoothEditParam(self);
  initBoothEditParam(booth);
  tiyData.tStatus.booth.modalModify.isShow = true;
}

/**保存修改展区
 * @param callback 回调函数
 * */
export const modifyBooth = (self) => {
  self.$refs.BoothEdit.$refs.boothInfoForm.validate(valid => {
    if(valid) {
      let params = tiyData.tParam.editParam.booth;
      if(params.displayType == commonData.boothDic.htmlType) {
        params.htmlContent = replaceHtmlTagAttr(params.htmlContent, "img", "width", "auto");
        params.htmlContent = replaceHtmlTagAttr(params.htmlContent, "img", "height", "auto");
      }
      self.$Spin.show();
      self.$apiStand.updateBooth(params).then(response => {
        if(response.success) {
          afterModifyBoothBaseInfo(self, params);
        }
      });
    }
  });
}

/**展区修改保存成功之后*/
export const afterModifyBoothBaseInfo = (self, booth) => {
  //刷新展区列表
  queryBoothList(self);
  //刷新展区基础信息
  initBoothInfo(self, booth.id, false);
  //关闭弹窗
  tiyData.tStatus.booth.modalModify.isShow = false;
}

/**查询展位列表*/
export const queryBoothItemList = (self) => {
  tiyData.tParam.searchParam.boothItem.boothId = tiyData.tData.boothBaseInfo.id;
  let params = {
    boothId: tiyData.tParam.searchParam.boothItem.boothId
  };
  if(tiyData.tParam.searchParam.boothItem.targetType != 0) {
    params.targetType = tiyData.tParam.searchParam.boothItem.targetType;
  }
  self.$Spin.show();
  self.$apiStand.queryBoothItemList(params).then(response => {
    tiyData.tData.boothItemList.splice(0);
    if(response.success) {
      tiyData.tData.boothItemList.push(...response.result);
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**显示新增展位弹窗*/
export const showAddBoothItemModal = (self) => {
  let isNeedValidate = false;
  let maxCount = 0;
  switch (tiyData.tData.boothBaseInfo.displayType) {
    case commonData.boothDic.singleType: //单行单图
      isNeedValidate = true;
      maxCount = 1;
      break;
    case commonData.boothDic.left1Right2Type: //左一右二
    case commonData.boothDic.left2Right1Type: //左二右一
      isNeedValidate = true;
      maxCount = 3;
      break;
    default: {
      isNeedValidate = false;
      break;
    }
  }
  if(isNeedValidate) {
    self.$Spin.show();
    let params = {boothId: tiyData.tData.boothBaseInfo.id};
    self.$apiStand.getBoothItemCount(params).then(response => {
      self.$Spin.hide();
      if(response.success) {
        if(response.result >= maxCount) {
          self.$Message.warning({content:"展位已满！"+getDisplayTypeName(tiyData.tData.boothBaseInfo.displayType)+"类型展区最多只能有"+maxCount+"个展位", duration: 5});
        }else{
          resetBoothItemAddParam(self);
          tiyData.tParam.addParam.boothItem.showroomId = tiyData.tData.boothBaseInfo.showroomId;
          tiyData.tStatus.boothItem.modalAdd.isShow = true;
        }
      }
    });
  }else{
    resetBoothItemAddParam(self);
    tiyData.tParam.addParam.boothItem.showroomId = tiyData.tData.boothBaseInfo.showroomId;
    tiyData.tStatus.boothItem.modalAdd.isShow = true;
  }
}

const resetBoothItemAddParam = (self) => {
  /*tiyData.tParam.addParam.boothItem.showroomId = "";
  tiyData.tParam.addParam.boothItem.boothId = "";
  tiyData.tParam.addParam.boothItem.title = "";
  tiyData.tParam.addParam.boothItem.sequence = 10;
  tiyData.tParam.addParam.boothItem.imagePc = "";
  tiyData.tParam.addParam.boothItem.imagePcAlt = "";
  tiyData.tParam.addParam.boothItem.imageMobile = "";
  tiyData.tParam.addParam.boothItem.imageMobileAlt = "";*/
  self.$refs.BoothItemAdd.$refs.boothItemInfoForm.resetFields();
  tiyData.tParam.addParam.boothItem.sequence = 10;
}

const getDisplayTypeName = (type) => {
  let element = commonData.boothDic.types.find(element => element.value == type);
  return element==null?"":element.label;
}

/**保存新增展位*/
export const addBoothItem = (self) => {
  self.$refs.BoothItemAdd.$refs.boothItemInfoForm.validate(valid => {
    if(valid) {
      tiyData.tParam.addParam.boothItem.boothId = tiyData.tData.boothBaseInfo.id;
      let params = tiyData.tParam.addParam.boothItem;
      self.$Spin.show();
      self.$apiStand.saveBoothItem(params).then(response => {
        if(response.success) {
          self.$Message.success("保存新增展条目位成功！");
          //关闭弹窗
          tiyData.tStatus.boothItem.modalAdd.isShow = false;
          //刷新展厅信息和展位列表
          refreshStandsAndBoothItems(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

export const refreshStandsAndBoothItems = (self) => {
  //刷新展厅最后修改、最后发布时间信息
  afterModifyStandsBaseInfo(self);
  //刷新展位列表
  queryBoothItemList(self);
}

export const showModifyBoothItemModal = (self, boothItemId) => {
  tiyData.tStatus.boothItem.modalModify.isShow = true;
  resetBoothItemEditParam(self);
  let params = {id: boothItemId};
  self.$Spin.show();
  self.$apiStand.queryBoothItemById(params).then(response => {
    if(response.success) {
      initBoothItemEditParam(self, response.result);
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

export const resetBoothItemEditParam = (self) => {
  /*tiyData.tParam.editParam.boothItem.showroomId = "";
  tiyData.tParam.editParam.boothItem.boothId = "";
  tiyData.tParam.editParam.boothItem.title = "";
  tiyData.tParam.editParam.boothItem.sequence = 0;
  tiyData.tParam.editParam.boothItem.imagePc = "";
  tiyData.tParam.editParam.boothItem.imagePcAlt = "";
  tiyData.tParam.editParam.boothItem.imageMobile = "";
  tiyData.tParam.editParam.boothItem.imageMobileAlt = "";*/
  self.$refs.BoothItemEdit.$refs.boothItemInfoForm.resetFields();
}

export const initBoothItemEditParam = (self, boothItem) => {
  tiyData.tParam.editParam.boothItem.id = boothItem.id;
  tiyData.tParam.editParam.boothItem.showroomId = boothItem.showroomId;
  tiyData.tParam.editParam.boothItem.boothId = boothItem.boothId;
  tiyData.tParam.editParam.boothItem.targetType = boothItem.targetType;
  tiyData.tParam.editParam.boothItem.sequence = boothItem.sequence;
  tiyData.tParam.editParam.boothItem.title = boothItem.title;
  tiyData.tParam.editParam.boothItem.imagePc = boothItem.imagePc;
  tiyData.tParam.editParam.boothItem.imageMobile = boothItem.imageMobile;
  tiyData.tParam.editParam.boothItem.imagePcAlt = boothItem.imagePcAlt;
  tiyData.tParam.editParam.boothItem.imageMobileAlt = boothItem.imageMobileAlt;
  tiyData.tParam.editParam.boothItem.targetValue = boothItem.targetValue;
  if(boothItem.targetType == commonData.boothItemDic.collectionType && boothItem.collection != null) {
    tiyData.tParam.editParam.boothItem.collectionTitle = boothItem.collection.title;
    tiyData.tParam.editParam.boothItem.collectionType = boothItem.collection.type;
    tiyData.tParam.editParam.boothItem.collectionKeyword = boothItem.collection.keyword;
    tiyData.tParam.editParam.boothItem.collectionFilters.splice(0);
    if(isNotEmpty(boothItem.collection.filters)) {
      tiyData.tParam.editParam.boothItem.collectionFilters.push(...self.$toJson(boothItem.collection.filters));
    }
  }
}

/**删除展位*/
export const deleteBoothItem = (self, boothItemId) => {
  self.$Modal.confirm({
    title: "确认",
    content: "确定删除此展位吗？",
    okText: "删除",
    cancelText: "取消",
    onOk() {
      let params = {id: boothItemId};
      self.$Spin.show();
      self.$apiStand.deleteBoothItem(params).then(response => {
        if(response.success) {
          self.$Message.success("展位删除成功！");
          //刷新展厅信息和展位列表
          refreshStandsAndBoothItems(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

/**修改展位基本信息*/
export const modifyBoothItem = (self) => {
  self.$refs.BoothItemEdit.$refs.boothItemInfoForm.validate(valid => {
    if(valid) {
      let params = tiyData.tParam.editParam.boothItem;
      self.$Spin.show();
      self.$apiStand.updateBoothItem(params).then(response => {
        if(response.success) {
          self.$Message.success("展位修改成功！");
          //关闭弹窗
          tiyData.tStatus.boothItem.modalModify.isShow = false;
          //刷新展厅信息和展位列表
          refreshStandsAndBoothItems(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

export const showShowroomSelectionModal = (self, boothItem, boothPlatform) => {
  tiyData.tStatus.boothItem.modalShowroom.isShow = true;
  //初始化展厅选择列表查询参数
  initShowroomParams(boothPlatform);
  //初始化展厅选择列表分页参数
  initShowroomPage();
  //初始化展厅选择列表自定义参数
  initCustomParams(boothItem);
  //查询展厅列表
  showroomData.tData.list.splice(0);
  // queryShowroomList(self); //初始不再查询展厅
}

export const initCustomParams = (boothItem) => {
  //查询参数
  showroomData.tParam.exceptedId = boothItem.targetValue;
  //修改参数
  tiyData.tParam.editParam.showroom.boothItemId = boothItem.id;
  tiyData.tParam.editParam.showroom.showroomId = "";
}

/**查询可关联的展厅列表*/
export const queryShowroomList = (self) => {
  let params = showroomData.tParam;
  params.pageNum = showroomData.tPage.pageNum;
  params.pageSize = showroomData.tPage.pageSize;
  self.$ySpliceAll(showroomData.tData.list);
  self.$Spin.show();
  self.$apiStand.queryStandsList(params).then(response => {
    if(response.success) {
      let page = response.result;
      showroomData.tData.list.push(...page.list);
      showroomData.tPage.total = page.total;
    }else{
      showroomData.tPage.total = 0;
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**修改关联展厅*/
export const modifyShowroom = (self) => {
  if(showroomData.tData.selectedList.length == 0) {
    self.$Message.error("请选择展厅！");
    return;
  }
  tiyData.tParam.editParam.showroom.showroomId = showroomData.tData.selectedList[0].id;
  let params = tiyData.tParam.editParam.showroom;
  self.$Spin.show();
  self.$apiStand.updateShowroomOfBoothItem(params).then(response => {
    if(response.success) {
      self.$Message.success("关联展厅成功！");
      //关闭弹窗
      tiyData.tStatus.boothItem.modalShowroom.isShow = false;
      //刷新展厅信息和展位列表
      refreshStandsAndBoothItems(self);
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

export const modifyBoothItemHtml = (self) => {
  self.$refs.itemHtmlForm.validate(valid => {
    if(valid) {
      let params = {
        boothItemId: tiyData.tParam.editParam.boothItem.id,
        htmlLink: tiyData.tParam.editParam.boothItem.targetValue
      };
      self.$Spin.show();
      self.$apiStand.updateHtmlOfBoothItem(params).then(response => {
        self.$Spin.hide();
        if(response.success) {
          tiyData.tStatus.boothItem.modalHtml.isShow = false;
          queryBoothItemList(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
      });
    }else{
      console.log("validate failed")
    }
  })
}

export const showModifyCollectionModal = (self, boothItem) => {
  self.$refs.modifyCollection.$refs.collectionForm.resetFields();
  collectionData.tParam.addParam.collection.boothItemId = boothItem.id;
  collectionData.tParam.addParam.collection.hasDate=false;
  collectionData.tParam.addParam.collection.hasKeyword=false;
  collectionData.tParam.addParam.collection.publishedStartDate="";
  collectionData.tParam.addParam.collection.publishedEndDate="";
  collectionData.tStatus.collection.modalAdd.isShow = true;
}

export const modifyCollection = (self) => {
  self.$refs.modifyCollection.$refs.collectionForm.validate(valid => {
    if(valid) {
      let params = collectionData.tParam.addParam.collection;
      if(params.type == commonData.collectionDic.dateRangeType) {
        if(params.keyword == "[\'}" || params.keyword.indexOf("\'\'") > -1) {
          self.$Modal.warning({title: "警告", content:"请选择上架起始日期！"});
          return;
        }
      }
      //处理日期
      if(params.publishedStartDate && params.publishedStartDate instanceof Object){
        var localDate = params.publishedStartDate;
        params.publishedStartDate = new Date(Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(),0,0,0)).toJSON();
      }
      if(params.publishedEndDate && params.publishedEndDate instanceof Object){
        var localDate = params.publishedEndDate;
        params.publishedEndDate = new Date(Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(),0,0,0)).toJSON();
      }

      self.$Spin.show();
      self.$apiStand.saveOrUpdateCollection(params).then(response => {
        self.$Spin.hide();
        if(response.success) {
          collectionData.tStatus.collection.modalAdd.isShow = false;
          queryBoothItemList(self);
          if(params.type == commonData.collectionDic.customType) {
            showCollectionProductsModal(params.boothItemId, response.result);
          }
        }else{
          self.$Message.error(response.errorMessage);
        }
      })
    }else{
      console.log("validate failed")
    }
  });
}

export const showCollectionProductsModal = (boothItemId, collectionId) => {
  tiyData.tParam.editParam.collectionProducts.collectionId = collectionId;
  tiyData.tParam.editParam.collectionProducts.boothItemId = boothItemId;
  tiyData.tParam.editParam.collectionProducts.modifyingTime = new Date().getTime();
  tiyData.tStatus.boothItem.modalProducts.isShow = true;
}

export const queryAllProductList = (self) => {
  let params = productData.tParam;
  params.pageNum = productData.tPage.pageNum;
  params.pageSize = productData.tPage.pageSize;
  self.$Spin.show();
  self.$apiProduct.findProducts(params).then(response => {
    self.$Spin.hide();
    productData.tData.list.splice(0);
    productData.tData.selectedList.splice(0);
    if(response.success) {
      let page = response.result;
      productData.tData.list.push(...page.list);
      productData.tPage.total = page.total;
    }else{
      productData.tPage.total = 0;
    }
  });
}
